<?php
namespace app\common\model;
use think\Db;


class Device
{
    private $deviceId;
    private $deviceType;
    public function getDevice($deviceId){
        if(empty($deviceId)){
            return false;
        }
        $db=Db::connect();
        $tables=$db->getTables(config('database.database'));
        $prefix=config('database.prefix');
        $device=false;
        krsort($tables);
        foreach($tables as $v){
            if(match($v,"/^{$prefix}device_y_20[0-9][0-9]$/")){
                $device=$db->table($v)
                    ->where(['device_id'=>$deviceId])
                    ->find();
                if($device){
                    $redis=new Redis();
                    $redis->hMset('device:'.$deviceId,$device);
                    $redis->expire('device:'.$deviceId,config('device_ttl'));
                    break;
                }
            }
        }
        return $device;
    }


    public function addDevice($data){
        $device=$this->getDevice($data['device_id']);
        if($device){
            return true;
        }
        return $this->_insert($data);
    }

    private function _insert($input){
        if(defined('CLIENTID')){
            $clientId=CLIENTID;
        }else{
            $clientId=get_array_value($input,'client_id',0);
        }
        $data=[
            'device_id'=>get_array_value($input,'device_id',''),
            'channel_id'=>get_array_value($input,'channel_id',0),
            'cps_id'=>get_array_value($input,'cps_id',0),
            'game_id'=>get_array_value($input,'game_id',0),
            'active_time'=>get_array_value($input,'active_time',time()),
            'imsi'=>get_array_value($input,'imsi',''),
            'imei'=>get_array_value($input,'imei',''),
            'mac'=>get_array_value($input,'mac',''),
            'phone_model'=>get_array_value($input,'phone_model',''),
            'net_type'=>get_array_value($input,'net_type',''),
            'phone_width'=>get_array_value($input,'phone_width',0),
            'phone_height'=>get_array_value($input,'phone_height',0),
            'active_ip'=>get_array_value($input,'active_ip',request()->ip()),
            'device_type'=>get_array_value($input,'device_type','android'),
            'ver'=>get_array_value($input,'ver','v1.0'),
            'status'=>1,
            'client_id'=>$clientId

        ];
        if(!isset($data['channel_id'])){
            return false;
        }
        if(!isset($data['game_id'])){
            return false;
        }
        $year=date('Y',$data['active_time']);
        $db=Db::connect();
        $tables=$db->getTables(config('database.database'));
        $prefix=config('database.prefix');
        krsort($tables);
        $tableExists=false;
        foreach($tables as $v){
            if($v==$prefix.'device_y_'.$year){
                $tableExists=true;
                break;
            }
        }
        //如果表不存在就创建
        if(!$tableExists){
            $sql = "CREATE TABLE `{$prefix}device_y_{$year}` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `device_id` varchar(64) DEFAULT NULL,
  `channel_id` int(11) DEFAULT NULL,
  `cps_id` int(11) DEFAULT NULL,
  `game_id` int(11) DEFAULT NULL,
  `active_time` int(11) DEFAULT NULL,
  `imsi` varchar(32) DEFAULT NULL,
  `imei` varchar(32) DEFAULT NULL,
  `mac` varchar(39) DEFAULT NULL,
  `phone_model` varchar(16) DEFAULT NULL,
  `net_type` varchar(16) DEFAULT NULL,
  `phone_width` int(11) DEFAULT NULL,
  `phone_height` int(11) DEFAULT NULL,
  `active_ip` varchar(15) DEFAULT NULL,
  `device_type` varchar(16) DEFAULT NULL,
  `ver` varchar(16) DEFAULT NULL,
  `debug` varchar(16) DEFAULT NULL,
  `status` int(4) DEFAULT '1',
  `client_id` int(10) DEFAULT NULL COMMENT '客户端id',
  PRIMARY KEY (`id`),
  UNIQUE KEY `device_id` (`device_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=164748 DEFAULT CHARSET=utf8;";

            try{
                Db::execute($sql);//创建表
            }catch(\Exception $e){
                Log::record('create device table error.'.$e->getMessage());
            }
        }
        try{
            Db::name('device_y_'.$year)->insert($data);
            return true;
        }catch(\Exception $e){
            Log::record('device insert error.'.$e->getMessage()."|data:".json_encode($data));
            return false;
        }

    }
    public function isDisabled($deviceId){
        switch($deviceId){
            case '46d0738baaa9a16874593ef39c73e354'://1236十万个大魔王 用户 米娱ID:45185734  roleid:31171432
                return true;
            default:
                return false;
        }
    }


}



